package com.cskaoyan.app.wxcontroller;

import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.vo.WxUploadVO;
import com.cskaoyan.app.config.AliComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

@RestController
@RequestMapping("wx/storage")
public class WxStorageController {

    @Autowired
    private AliComponent aliComponent;

    @PostMapping("upload")
    public BaseRespVo<WxUploadVO> upload(MultipartFile file)
            throws IOException {
        String filename = file.getOriginalFilename();
        String baseName = filename.substring(0, filename.lastIndexOf("."));
        String contentType = file.getContentType();
        long size = file.getSize();
        InputStream inputStream = file.getInputStream();
        aliComponent.fileUpload(filename, inputStream);
        String url = aliComponent.getUrl(filename);
        WxUploadVO uploadVO = new WxUploadVO(0, baseName, filename, contentType, size, url,
                new Date(), new Date());
        return BaseRespVo.ok(uploadVO);
    }
}
